#include "stdio.h"
#include "string.h"
#include "hkdiscovery.h"
#include "hksadp.h"
#include "hkstruct.h"
#include "common.h"

int resetDfltPasswd(char *arg)
{
	return 0;
}

int NETCFGCB(unsigned int ip, unsigned int mask,unsigned int gateway,unsigned int port)
{
#if 0
	char *pIP = NULL;
	char ipaddr[16] = {0};
	char maskaddr[16] = {0};
	char gatewayip[16] = {0};
	struct in_addr addr = {0};
	HKCfgS hkcfgs = {0};
	conf_get_HKcfg(&hkcfgs);
	addr.s_addr = htonl(ip);
	pIP = inet_ntoa(addr);
	if(pIP)
	{
		memcpy(ipaddr,pIP,strlen(pIP));
	}
	addr.s_addr = htonl(mask);
	pIP = inet_ntoa(addr);
	if(pIP)
	{
		memcpy(maskaddr,pIP,strlen(pIP));
	}
	if(port!=0&&port!=(DWORD)hkcfgs.tcp_port)
	{
		hkcfgs.tcp_port = port;
		conf_set_HKstatus(&hkcfgs);
		re_init_hk_server();
	}
	gateway = htonl(ip);
	char *pgateway = (char*)&gateway;
	pgateway[3] = 1;
	addr.s_addr = gateway;
	pIP = inet_ntoa(addr);
	if(pIP)
	{		
		memcpy(gatewayip,pIP,strlen(pIP));	
	}
	char msgbuf[512] = {0};
    sprintf(msgbuf, "ethcfg -act set -ethip %s -ethmask %s -ethgw %s \r\n",
		ipaddr,maskaddr, gatewayip);
	char respbuf[512] = {0,};
	jcpcmd_sendrecv(msgbuf, respbuf, sizeof(respbuf));
#endif	
	return 0;
}


int initSADP(void)
{
	
	char *p = NULL;
	char macaddr[8] = {0};
	dev_info info = {{0}};

	ProtocolAPIHandle_T *handle = GetProtocolAPIHandle();
	NetWorkInfo_T netInfo = {0};
	NetProtocolInfo_T protocolInfo = {0};
	DeviceInfo_T  deviceInfo = {0};
	APIISNULL(handle->pGetNetWorkInfo);
	APIISNULL(handle->pGetDeviceInfo);
	APIISNULL(handle->pGetNetProtocolInfo);
	handle->pGetNetWorkInfo(&netInfo);
	handle->pGetDeviceInfo(&deviceInfo);
	handle->pGetNetProtocolInfo(&protocolInfo);

	snprintf(info.serial_no,SOFTWARE_VERSION_LEN,"%s",deviceInfo.serialNumber);
	snprintf(info.software_version,SOFTWARE_VERSION_LEN,"%s",deviceInfo.softwareVer);
	snprintf(info.dsp_software_version,SOFTWARE_VERSION_LEN,"%s",deviceInfo.softwareVer);
	snprintf(info.start_time,SOFTWARE_VERSION_LEN,"%s","2017-03-07 08:08:08");
	info.dev_type = 0x022534;
	info.enc_cnt = 0;
	info.port = protocolInfo.HaiKangPort;
	info.hdisk_cnt = ntohl(0);
	int i = 0;

	if((p=strtok(netInfo.mac, ":")) != NULL)
	{
		macaddr[i++] = strtol(p,NULL,16);
		while((p=strtok(NULL, ":")) != NULL)
		{
			macaddr[i++] = strtol(p,NULL,16);
		}
	}
	init_sadp_lib(ntohl(inet_addr(netInfo.ip)),ntohl(inet_addr(netInfo.gateway)),(BYTE*)macaddr,(char*)"admin",
		strlen((char*)"admin"),&info, NETCFGCB,resetDfltPasswd);	
	start_sadp_cap();
	sleep(1);
	sadp_login();
	return 0;
}

int HKDoDiscoverRequest(char *msg,int len) 
{
	
}
